<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Помошник для работы с HTTP протоколом
 *
 * @package CoreFramework
 * @version 1.0
 * @author Gennadiy Kozlenko
 */

/**
 * Получение контента с помощью GET запроса
 *
 * @param string $url
 * @param array $headers
 * @param int $timeout
 * @param string $cookie
 * @param array $cookies
 * @param array $proxy
 * @return mixed
 */
function get_content($url, $headers = array(), $timeout = 0, $cookie = '', $cookies = array(), $proxy = array()) {
    $content = false;
    if (false !== ($ch = curl_init())) {
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_ENCODING, '');
        if (0 < $timeout) {
            curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout * 100);
        }
        if (!empty($cookie)) {
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
        }
        if (!empty($cookies)) {
            $array = array();
            foreach ($cookies as $key => $val) {
                array_push($array, "$key=$val");
            }
            curl_setopt($ch, CURLOPT_COOKIE, implode('; ', $array));
        }
        if (!empty($proxy)) {
            curl_setopt($ch, CURLOPT_PROXY, $proxy['proxy']);
            if (!empty($proxy['user']) && !empty($proxy['pass'])) {
                curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy['user'] . ':' . $proxy['pass']);
            }
        }
        $content = curl_exec($ch);
        curl_close($ch);
    }
    return $content;
}

/**
 * Получение контента с помощью POST запроса
 *
 * @param string $url
 * @param array $post
 * @param array $headers
 * @param int $timeout
 * @param string $cookie
 * @param array $cookies
 * @param array $proxy
 * @return mixed
 */
function post_content($url, $post, $headers = array(), $timeout = 0, $cookie = '', $cookies = array(), $proxy = array()) {
    $content = false;
    if (false !== ($ch = curl_init())) {
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        curl_setopt($ch, CURLOPT_ENCODING, '');
        if (0 < $timeout) {
            curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout * 100);
        }
        if (!empty($cookie)) {
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
        }
        if (!empty($cookies)) {
            $array = array();
            foreach ($cookies as $key => $val) {
                array_push($array, "$key=$val");
            }
            curl_setopt($ch, CURLOPT_COOKIE, implode('; ', $array));
        }
        if (!empty($proxy)) {
            curl_setopt($ch, CURLOPT_PROXY, $proxy['proxy']);
            if (!empty($proxy['user']) && !empty($proxy['pass'])) {
                curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy['user'] . ':' . $proxy['pass']);
            }
        }
        $content = curl_exec($ch);
        curl_close($ch);
    }
    return $content;
}

/**
 * Получение информации по заголовкам
 *
 * @param string $url
 * @param array $headers
 * @param int $timeout
 * @package string $cookie
 * @param array $cookies
 * @param array $proxy
 * @return array
 */
function head_info($url, $headers = array(), $timeout = 0, $cookie = '', $cookies = array(), $proxy = array()) {
    $info = false;
    if (false !== ($ch = curl_init())) {
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_ENCODING, '');
        if (0 < $timeout) {
            curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout * 100);
        }
        if (!empty($cookie)) {
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
        }
        if (!empty($cookies)) {
            $array = array();
            foreach ($cookies as $key => $val) {
                array_push($array, "$key=$val");
            }
            curl_setopt($ch, CURLOPT_COOKIE, implode('; ', $array));
        }
        if (!empty($proxy)) {
            curl_setopt($ch, CURLOPT_PROXY, $proxy['proxy']);
            if (!empty($proxy['user']) && !empty($proxy['pass'])) {
                curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy['user'] . ':' . $proxy['pass']);
            }
        }
        curl_exec($ch);
        $info = array();
        if (CURLE_OK != curl_errno($ch)) {
            $info['errno'] = curl_errno($ch);
            $info['error'] = curl_error($ch);
        }
        $info = array_merge($info, curl_getinfo($ch));
        curl_close($ch);
    }
    return $info;
}

/**
 * Get random proxy
 */
function get_random_proxy() {
    $obj =& get_instance();
    $obj->load->config('proxy');
    $proxy = array();
    if ($obj->config->item('use_proxy')) {
        $list = $obj->config->item('proxies_list');
        if (count($list) > 0) {
            $proxy = $list[rand(0, count($list) - 1)];
        }
    }
    return $proxy;
}
